/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.acworks.acroute.v2.impl;

import com.acworks.acroute.v2.Arc;
import com.acworks.acroute.v2.ArcDirectionType;
import com.acworks.acroute.v2.Node;
import java.util.Set;

/**
 *
 * @author nikita
 */
public class ArcImpl implements Arc {

    private Node sourceNode;
    private Node targetNode;
    private ArcDirectionType type;
    private Set<ArcChangeListener> arcChangeListeners;

    public ArcImpl(Node sourceNode, Node targetNode, ArcDirectionType type) {
        assert sourceNode != null;
        assert targetNode != null;
        assert type != null;

        this.sourceNode = sourceNode;
        this.targetNode = targetNode;
        this.type = type;
    }

    ArcImpl(Arc arc) {
        assert arc != null;

        this.type = arc.getType();
        this.sourceNode = arc.getSourceNode();
        this.targetNode = arc.getTargetNode();
    }

    @Override
    public boolean makeDirected(Node originNode) {
        if (!sourceNode.equals(originNode) && !targetNode.equals(originNode)) {
            throw new IllegalArgumentException("node isn't connected to this arc: " + originNode);
        }

        boolean altered = false;

        if (!sourceNode.equals(originNode)) {
            targetNode = sourceNode;
            sourceNode = originNode;

            altered = true;
        }
        if (type != ArcDirectionType.DIRECTED) {
            type = ArcDirectionType.DIRECTED;
            altered = true;
        }

        return altered;
    }

    @Override
    public Node getSourceNode() {
        return sourceNode;
    }

    @Override
    public Node getTargetNode() {
        return targetNode;
    }

    @Override
    public ArcDirectionType getType() {
        return type;
    }

    @Override
    public boolean isIncident(Node node) {
        assert node != null;
        return node.equals(getSourceNode()) || node.equals(getTargetNode());
    }

    @Override
    public String toString() {
        return super.toString() + "[sourceNode=" + sourceNode + ", targetNode=" + targetNode + ", " +
                "type=" + type + "]";
    }

    /**
     * @return the arcChangeListeners
     */
    public Set<ArcChangeListener> getArcChangeListeners() {
        return arcChangeListeners;
    }
//    @Override
//    public int compareTo(Arc arc) {
//        int compareSource = getSourceNode().compareTo(arc.getSourceNode());
//
//        if (compareSource == 0) {
//            return getTargetNode().compareTo(arc.getTargetNode());
//        } else {
//            return compareSource;
//        }
//
//    }
}
